package com.ciwong.epaper.modules.homework.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.clazz.bean.TeacherClassBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wyk
 * @time: 2022/2/23 19:18
 */
public class WorkClazzAdapter extends BaseAdapter {

    private List<TeacherClassBean> teacherClassBeans = new ArrayList<>();
    private Context mContext;
    private int classId;


    public WorkClazzAdapter(Activity context,List<TeacherClassBean> teacherClassBeans,int classId){
        this.mContext = context;
        this.teacherClassBeans = teacherClassBeans;
        this.classId = classId;
    }

    @Override
    public int getCount() {
        return null == teacherClassBeans ? 0 : teacherClassBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return null == teacherClassBeans ? null : teacherClassBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (null == view){
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_work_clazz,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.work_clazz_lay = view.findViewById(R.id.work_clazz_lay);
            viewHolder.work_clazz_text = view.findViewById(R.id.work_clazz_text);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        setView(viewHolder,i);
        return view;
    }

    private void setView(ViewHolder viewHolder,int index){
        if (teacherClassBeans.get(index).getClassId() == classId){
            viewHolder.work_clazz_text.setTextColor(mContext.getResources().getColor(R.color.white));
            viewHolder.work_clazz_lay.setBackground(mContext.getResources().getDrawable(R.color.title_color));
        }else {
            viewHolder.work_clazz_text.setTextColor(mContext.getResources().getColor(R.color.black));
            viewHolder.work_clazz_lay.setBackground(mContext.getResources().getDrawable(R.color.def_line_color));
        }
        viewHolder.work_clazz_text.setText(teacherClassBeans.get(index).getClassName());
    }

    public void setNewData(List<TeacherClassBean> teacherClassBeans,int classId){
        this.teacherClassBeans = teacherClassBeans;
        this.classId = classId;
        notifyDataSetChanged();
    }

    private static class ViewHolder{
        private RelativeLayout work_clazz_lay;
        private TextView work_clazz_text;
    }
}
